package com.example.smartcity_poverty.ui.AllService.RubbishClassify.fragment

import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity_poverty.App.Companion.getToken
import com.example.smartcity_poverty.App.Companion.url
import com.example.smartcity_poverty.Base.BaseRecyclerAdapter
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.model.RubbishClassify.RcNewsListModel
import com.example.smartcity_poverty.logic.util.Util.glide
import com.example.smartcity_poverty.logic.util.Util.show
import com.example.smartcity_poverty.ui.AllService.RubbishClassify.activity.RcShowNewsActivity
import kotlinx.android.synthetic.main.fragment_news3.*
import kotlinx.android.synthetic.main.re_rc_news_list.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class RcNewsFragment : Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_news3, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        // 配置新闻分类
        setRcNews(RcIdNumber)
    }

    /**
     * 垃圾分类新闻展示
     */
    private fun setRcNews(rcIdNumber: Int) {
        // 判断token是否为空
        if (getToken().isNotEmpty()) {
            // 开启协程发起网络请求
            CoroutineScope(Dispatchers.IO).launch {
                // 获取新闻列表信息
                api.getRcNewsList(getToken(), rcIdNumber).apply {
                    // 判断是否发起网络成功！
                    if (this.code == 200) {
                        val data = this.rows
                        // 填充数据
                        activity?.runOnUiThread {
                            rcNewsListRv.apply {
                                this.isNestedScrollingEnabled = false
                                this.layoutManager = LinearLayoutManager(context)
                                this.adapter =
                                    RcNewsAdapter(ArrayList<RcNewsListModel.RowsBean>().apply {
                                        this.addAll(data!!)
                                    }, R.layout.re_rc_news_list)
                            }
                        }
                    } else activity?.runOnUiThread {
                        // 弹窗错误
                        this.msg?.show()
                    }
                }
            }
        } else "请先登录！".show()
    }

    /**
     * 新闻列表适配器
     */
    inner class RcNewsAdapter(
        val list: MutableList<RcNewsListModel.RowsBean>,
        layoutId: Int
    ) : BaseRecyclerAdapter<RcNewsListModel.RowsBean>(list, layoutId) {
        override fun setData(
            view: BaseViewHolder.ViewFind,
            data: RcNewsListModel.RowsBean,
            position: Int,
            holder: BaseViewHolder
        ) {
            val d = list[position]
            val fv = holder.viewFind
            val con = holder.viewFind.view?.context

            // 填充数据
            fv.rcNewsTitle.text = d.title
            fv.rcNewsTime.text = "发布时间: ${d.createTime}"
            glide(url + d.imgUrl, fv.rcNewsImage)

            // 点击进新闻详情，传输id给下一个Activity查询新闻详情
            holder.itemView.setOnClickListener {
                con?.startActivity(Intent(con, RcShowNewsActivity::class.java).putExtra("id", d.id.toString()))
            }
        }
    }

    companion object {
        var RcIdNumber = 0
    }
}